<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('时间')">
              <a-date-picker
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.bTime"
              ></a-date-picker>
              <span class="query-group-split-cust"></span>
              <a-date-picker
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.eTime"
              ></a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="6" :md="7" :sm="8">
            <a-form-item :label="$t('名称')">
              <a-input v-model="queryParam.ticketInfo" :placeholder="$t('请输入项目名称')"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="!tenantid">
            <a-form-item :label="$t('门店')">
              <a-select v-model="queryParam.shopCode">
                <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value"
                >{{ item.title }}<span v-if="item.value != ''">（{{ item.value }}）</span></a-select-option
                >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="9" :md="10" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-dropdown>
                <a-menu slot="overlay" @click="handleDateClick">
                  <a-menu-item key="1"> {{ $t('今日') }} </a-menu-item>
                  <a-menu-item key="2"> {{ $t('近三天') }} </a-menu-item>
                  <a-menu-item key="3"> {{ $t('近七天') }} </a-menu-item>
                  <a-menu-item key="4"> {{ $t('近一月') }} </a-menu-item>
                  <a-menu-item key="5"> {{ $t('近三月') }} </a-menu-item>
                  <a-menu-item key="6"> {{ $t('近半年') }} </a-menu-item>
                  <a-menu-item key="7"> {{ $t('本年') }} </a-menu-item>
                </a-menu>
                <a-button type="primary" style="margin-left: 8px">
                  {{ $t('快捷查询') }} <a-icon type="down"
                /></a-button>
              </a-dropdown>
              <a-button type="primary" @click="searchReset" style="margin-left: 8px">{{ $t('重置') }}</a-button>
              <a-button
                type="primary"
                :loading="exloading"
                style="margin-left: 8px"
                @click="handleExportXls($t('门票项目报表'))"
                v-has="'GameTicketSummaryController:export'"
                >{{ $t('导出') }}</a-button
              >
              <a-button type="primary" v-print="'#printme'" style="margin-left: 8px" v-has="'GameTicketSummaryController:print'">{{ $t('打印') }}</a-button>
              <a-popover :title="$t('自定义列')" trigger="click" placement="leftBottom">
                <template slot="content">
                  <a-checkbox-group
                    @change="onColSettingsChange"
                    v-model="settingColumns"
                    :defaultValue="settingColumns"
                  >
                    <a-row style="width: 400px">
                      <template v-for="(item, i) in defColumns">
                        <template v-if="item.key != 'rowIndex' && item.dataIndex != 'action'">
                          <a-col :span="12" :key="i">
                            <a-checkbox :value="item.dataIndex">
                              <j-ellipsis :value="item.title" :length="10"></j-ellipsis>
                            </a-checkbox>
                          </a-col>
                        </template>
                      </template>
                    </a-row>
                  </a-checkbox-group>
                </template>
                <a-button style="margin-left: 8px" type="setting">{{ $t('设置显示项') }}</a-button>
              </a-popover>
            </span>
          </a-col>
          <template v-if="toggleSearchStatus"> </template>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- table区域-begin -->
    <div id="printme">
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">{{
            $t('下载')
          }}</a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">{{ $t('详情') }}</a>
        </span>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import Vue from 'vue'
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
export default {
  name: 'TicketProjectReport',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      moment,
      description: '门票项目报表',
      tenantid: Vue.ls.get('TENANT_ID'),
      // 表头
      defColumns: [
        {
          title: this.$t('项目名称'),
          align: 'center',
          dataIndex: 'goodsName',
          customRender: (t, r) => {
            if (t == '合计') {
              return this.$t(t)
            }
            return t
          },
        },
        {
          title: this.$t('销售数量'),
          align: 'center',
          dataIndex: 'saleNum',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('销售金额'),
          align: 'center',
          dataIndex: 'sale',
          customRender: (t) => {
            return t.toFixed(2)
          },
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('扣年月票张数'),
          align: 'center',
          dataIndex: 'deductNum',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
      ],
      url: {
        list: '/report/day/gameTicketSummary/list',
        exportXlsUrl: '/report/day/gameTicketSummary/export',
      },
      isNoPageTotal: true,
      queryParam: {
        bTime: moment().format('YYYY-MM-DD 00:00:00'),
        eTime: moment().format('YYYY-MM-DD 23:59:59'),
        shopCode: '',
      },
      settingColumns: [],
      columns: [],
      shops: [],
    }
  },
  created() {
    this.initColumns()
    if (!this.tenantid) {
      let dtl = {
          title: this.$t('门店编码'),
          align: 'center',
          dataIndex: 'tenantId',
          width: 100,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        },
        dtl1 = {
          title: this.$t('门店名称'),
          align: 'center',
          dataIndex: 'shopCode_dictText',
          width: 140,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        }
      this.defColumns.push(dtl)
      this.defColumns.push(dtl1)
    }

  },
  methods: {
    initColumns() {
      //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
      if (localStorage.getItem('operateCoin') == 1) {
        this.defColumns.splice(this.defColumns.length - 1, 0, {
          title: this.$t('币兑换数量'),
          align: 'center',
          dataIndex: 'coinNum',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        }, {
          title: this.$t('扣币值'),
          align: 'center',
          dataIndex: 'coin',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        })
      }
      if (localStorage.getItem('operateLottery') == 1) {
        this.defColumns.splice(this.defColumns.length - 1, 0, {
          title: this.$t('彩票兑换数量'),
          align: 'center',
          dataIndex: 'lotteryNum',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        }, {
          title: this.$t('扣票值'),
          align: 'center',
          dataIndex: 'lottery',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        })
      }
      if (localStorage.getItem('operateCredit') == 1) {
        this.defColumns.splice(this.defColumns.length - 1, 0, {
          title: this.$t('积分兑换数量'),
          align: 'center',
          dataIndex: 'integralNum',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        }, {
          title: this.$t('扣积分值'),
          align: 'center',
          dataIndex: 'integral',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        })
      }
      if (localStorage.getItem('operateACoin') == 1) {
        this.defColumns.splice(this.defColumns.length - 1, 0, {
          title: this.$t('A币兑换数量'),
          align: 'center',
          dataIndex: 'pointNum',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        }, {
          title: this.$t('扣A币'),
          align: 'center',
          dataIndex: 'point',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        })
      }
      if (localStorage.getItem('operateALottery') == 1) {
        this.defColumns.splice(this.defColumns.length - 1, 0, {
          title: this.$t('A票兑换数量'),
          align: 'center',
          dataIndex: 'alotteryNum',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        }, {
          title: this.$t('扣A票'),
          align: 'center',
          dataIndex: 'alottery',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        })
      }
      //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');
      var key = this.$route.name + ':colsettings'
      let colSettings = Vue.ls.get(key)
      if (colSettings == null || colSettings == undefined) {
        let allSettingColumns = []
        this.defColumns.forEach(function (item, i, array) {
          allSettingColumns.push(item.dataIndex)
        })
        this.settingColumns = allSettingColumns
        this.columns = this.defColumns
      } else {
        this.settingColumns = colSettings
        const cols = this.defColumns.filter((item) => {
          if (item.key == 'rowIndex' || item.dataIndex == 'action') {
            return true
          }
          if (colSettings.includes(item.dataIndex)) {
            return true
          }
          return false
        })
        this.columns = cols
      }
    },
    //列设置更改事件
    onColSettingsChange(checkedValues) {
      var key = this.$route.name + ':colsettings'
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
      this.settingColumns = checkedValues
      const cols = this.defColumns.filter((item) => {
        if (item.key == 'rowIndex' || item.dataIndex == 'action') {
          return true
        }
        if (this.settingColumns.includes(item.dataIndex)) {
          return true
        }
        return false
      })
      this.columns = cols
    },
    initDictConfig() {
      initDictOptions('bis_shop,name,code').then((res) => {
        if (res.success) {
          this.shops = res.result
          this.shops.unshift({ value: '', title: this.$t('全部') })
        }
      })
    },
    searchReset() {
      let that = this
      that.queryParam = {
        bTime: moment().format('YYYY-MM-DD 00:00:00'),
        eTime: moment().format('YYYY-MM-DD 23:59:59'),
        shopCode: '',
      }
      that.searchQuery()
    },
    customRow(r, i) {
      return {
        on:{
          click: () => {
            let x = this.selectedRowIndex.findIndex(r=>{return r=='a'+this.ipagination.current+''+i})
            if( x == -1 ){
              this.selectedRowIndex.push('a'+this.ipagination.current+''+i)
            }else{
              this.selectedRowIndex.splice(x, 1)
            }
          }
        },
        style: {
          whiteSpace: 'break-spaces',
          wordBreak: 'break-word',
          overflowWrap: 'break-word',
          backgroundColor: this.selectedRowIndex.findIndex(r=>{return r=='a'+this.ipagination.current+''+i}) > -1 ? '#FFECE6' : '',
          fontWeight: r.goodsName == '合计' ? 700 : 400,
        },
      }
    },
    handleDateClick(evt) {
      if (evt.key == '1') {
        this.queryParam.bTime = moment().format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '2') {
        this.queryParam.bTime = moment().subtract(2, 'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '3') {
        this.queryParam.bTime = moment().subtract(6, 'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '4') {
        this.queryParam.bTime = moment().subtract(1, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '5') {
        this.queryParam.bTime = moment().subtract(3, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '6') {
        this.queryParam.bTime = moment().subtract(6, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '7') {
        this.queryParam.bTime = moment().startOf('year').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      this.searchQuery()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>